    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.util;

import java.security.CodeSource;

import ch.zarzu.champions.builder.App;
import ch.zarzu.champions.builder.Main;

public class AppPath {
    private static String appPath = null;
    
    public static String get()
    {
        if(appPath == null) {
            if (App.IS_OSX && !App.IS_DEV) {
                try {
                    // find the current class, and then get the source and then the path
                    Class c = Class.forName(Main.class.getName());
                    CodeSource source = c.getProtectionDomain().getCodeSource();
                    
                    appPath = source.getLocation().getPath().replace("championBuilder.jar", "");
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                String programName = "championBuilder.exe";
                String pathSep = System.getProperties().getProperty("path.separator");
            
                String[] classPath = System.getProperties().getProperty("java.class.path").split(pathSep);
                for (String cp: classPath)
                {
                if (cp.endsWith(programName))
                return(cp.substring(0, (cp.length() - programName.length())));
                }
            
                String pwd = (System.getProperties().getProperty("user.dir") + System.getProperties().getProperty("file.separator"));
                
                if(App.DIST.equals("exe"))
                    appPath = pwd;
                else
                    appPath = "";
            }
        }
        
        return appPath;
    }
}
